package com.yuexunit.finance.member.action.v1;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Singleton;
import com.yuexunit.finance.member.action.v1.model.DaysEarningBaseDetail;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.web.annotation.ActionServlet;
import com.yuexunit.web.annotation.Permission;

import net.sf.jxls.transformer.XLSTransformer;

/**
 *@version 创建时间：2018年01月04日 上午10:00:06
 *@author kuangwj
 *类说明
 */

@ActionServlet({"/api/[^/]+/exportYesterdaysEarnings.json"})
@Singleton
@Permission
public class ExportYesterdaysEarnings extends HttpServlet {
	
	public static Logger logger = LoggerFactory.getLogger(ExportYesterdaysEarnings.class);
	
	private static final long serialVersionUID = 1L;

	private DaysEarningActionApi daysEarningActionApi = ComponentFactory.loadClass(DaysEarningActionApi.class);
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String interestDates = request.getParameter("interestDates");
			String createDateTimeEnd = request.getParameter("createDateTimeEnd");
			String financeNumber = request.getParameter("financeNumber");
			String enterpriseName = new String(request.getParameter("enterpriseName").getBytes("iso8859-1"),"utf-8");
			Date createDateTimeBegins=null;
			if(interestDates !=null){
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
				createDateTimeBegins=sdf.parse(interestDates);
			}
			Date createDateTimeEnds=null;
			if(createDateTimeEnd !=""){
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
				createDateTimeEnds= sdf.parse(createDateTimeEnd);
			}
			PageCriteria page = PageCriteria.defaultInstance();
			page.setPageSize(100000);
			PageList<DaysEarningBaseDetail> days = daysEarningActionApi.inquireDaysEarningPageList(null, enterpriseName, createDateTimeBegins, createDateTimeEnds, null, financeNumber, null, page);
			Map<String,Object> beans = new HashMap<String,Object>();  
			beans.put("arr", days);  
			XLSTransformer transformer = new XLSTransformer();  

			InputStream in=null;  
			OutputStream out=null;  
			//设置响应 
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("服务费收益_(时间).xls").getBytes("GB2312"), "ISO_8859_1"));  
			response.setContentType("application/vnd.ms-excel");

			in = new BufferedInputStream(new FileInputStream("C:\\Finance\\upload\\update\\yesterdaysEarnings.xls"));  
			Workbook workbook = transformer.transformXLS(in, beans);
			logger.info("in------"+in);
			out=response.getOutputStream();  
			workbook.write(out);  
			logger.info("out-----"+out);
			logger.info("workbook------"+workbook);
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
